//
//  UIButton+Extension.swift
//  Supermarket
//
//  Created by liuxm on 2017/9/25.
//  Copyright © 2017年 kushao. All rights reserved.
//

import Foundation
import UIKit

extension UIButton {
    func setIconInLeftWithSpacing(spacing: CGFloat) {
        self.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0)
        self.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0)
    }
    
    func setIconInRightWithSpacing(spacing: CGFloat) {
        let imgW = self.imageView?.frame.size.width
        let titleW = self.titleLabel?.frame.size.width
        
        self.titleEdgeInsets = UIEdgeInsetsMake(0, -(imgW! + spacing/2), 0, imgW! + spacing/2)
        self.imageEdgeInsets = UIEdgeInsetsMake(0, titleW! + spacing/2, 0, -(titleW! + spacing/2))
    }
    
    func setIconInTopWithSpacing(spacing: CGFloat) {
        let imgW = self.imageView?.frame.size.width
        let imgH = self.imageView?.frame.size.height
        let titleW = self.titleLabel?.frame.size.width
        let titleH = self.titleLabel?.frame.size.height
        
        self.titleEdgeInsets = UIEdgeInsets(top: (titleH! + spacing)/2, left: -(imgW!)/2, bottom: -(titleH! + spacing)/2, right: imgW!/2)
        self.imageEdgeInsets = UIEdgeInsets(top: -(imgH! + spacing)/2, left: titleW!/2, bottom: (imgH! + spacing)/2, right: -(titleW!/2))
    }
    
    func setIconInBottomWithSpacing(spacing: CGFloat) {
        let imgW = self.imageView?.frame.size.width
        let imgH = self.imageView?.frame.size.height
        let titleW = self.titleLabel?.frame.size.width
        let titleH = self.titleLabel?.frame.size.height
        
        self.titleEdgeInsets = UIEdgeInsetsMake(-(titleH! + spacing)/2, -(imgW!/2), (titleH! + spacing)/2, imgW!/2)
        self.imageEdgeInsets = UIEdgeInsetsMake((imgH! + spacing)/2, titleW!/2, -(imgH! + spacing)/2, -(titleW!/2))
    }
}

